package com.wutao.panzh.admin.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panzh.entity.HotelClassification;
import com.panzh.entity.HotelItemImag;
import com.panzh.entity.HotelItemSpu;
import com.panzh.entity.RouteItem;
import com.wutao.panzh.admin.mapper.HotelClassificationMapper;
import com.wutao.panzh.admin.mapper.HotelItemImagMapper;
import com.wutao.panzh.admin.mapper.HotelItemSpuMapper;
import com.wutao.panzh.service.HotelItemSpuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class HotelItemSpuServiceImpl implements HotelItemSpuService {

    @Autowired
    private HotelItemSpuMapper hotelItemSpuMapper;

    @Autowired
    private HotelItemImagMapper hotelItemImagMapper;

    @Autowired
    private HotelClassificationMapper hotelClassificationMapper;


    @Override
    public void saveHotelItem(HotelItemSpu hotelItemSpu) {
        //先查找出分类的id；
        HotelClassification hotelClassification = new HotelClassification();
        hotelClassification.setCategorizationInformation(hotelItemSpu.getCategorizationInformationName());
        hotelClassification = hotelClassificationMapper.selectOne(hotelClassification);
        //进行分类id的插入
        hotelItemSpu.setCategorizationInformationId(hotelClassification.getId());
        //进行hotelItemSpu本身的插入；
        hotelItemSpuMapper.insertHoteItemSpu(hotelItemSpu);
        //获得插入之后的id；
        Integer id = hotelItemSpuMapper.selectHotelItemSpuIdByhotelItemSpu(hotelItemSpu.getHotelItemSpuName());
        //进行图片的插入；
        List<HotelItemImag> imags = hotelItemSpu.getImags();
        for (HotelItemImag imag : imags) {
            imag.setHotelItemId(id);
            hotelItemImagMapper.insert(imag);
        }
    }

    @Override
    public PageInfo<HotelItemSpu> getHotelItemPageInfo(Integer pageNo, Integer pageSize, String keyword) {
        //使用pageHelper进行分页
        PageHelper.startPage(pageNo,pageSize);
        List<HotelItemSpu> hotelItems =  hotelItemSpuMapper.selectFroSearch(keyword);
        PageInfo<HotelItemSpu> pageInfo = new PageInfo<>(hotelItems);
        return pageInfo;
    }

    @Override
    public HotelItemSpu getHotelItemById(String hotelItemId) {
        //一定要考虑照片和分类的id，这两个东西容易出问题；
        HotelItemSpu hotelItemSpu = hotelItemSpuMapper.selectHotelItemSpuByhotelItemSpu(hotelItemId);
        return hotelItemSpu;
    }

    @Override
    public void deleteHotelItem(String hotelItemId) {
        //删除信息；
        HotelItemSpu hotelItemSpu = new HotelItemSpu();
        hotelItemSpu.setId(Integer.parseInt(hotelItemId));
        hotelItemSpuMapper.delete(hotelItemSpu);
        //删除图片，要删除就要删干净；
        HotelItemImag hotelItemImag = new HotelItemImag();
        hotelItemImag.setHotelItemId(Integer.parseInt(hotelItemId));
        hotelItemImagMapper.delete(hotelItemImag);

    }

    @Override
    public List<HotelItemSpu> getHotelAllByItemName(String hotelItemSpuName) {
        //这里要返回图片的信息，所以不能简单的使用通用mapper来使用；
        List<HotelItemSpu> list = hotelItemSpuMapper.selectAllByItemName(hotelItemSpuName);
        return list;
    }

    //插入的代码就可以使用保存的代码进行复制可以得到；
    @Override
    public void updateHotelItem(HotelItemSpu hotelItemSpu) {
        //先查找出分类的id；
        HotelClassification hotelClassification = new HotelClassification();
        hotelClassification.setCategorizationInformation(hotelItemSpu.getCategorizationInformationName());
        hotelClassification = hotelClassificationMapper.selectOne(hotelClassification);
        //进行分类id的插入
        hotelItemSpu.setCategorizationInformationId(hotelClassification.getId());
        //进行hotelItemSpu本身的插入；
        hotelItemSpuMapper.updateHoteItemSpu(hotelItemSpu);
        //获得插入之后的id；
        Integer id = hotelItemSpu.getId();
        //进行图片的插入；
        List<HotelItemImag> imags = hotelItemSpu.getImags();
        for (HotelItemImag imag : imags) {
            imag.setHotelItemId(id);
            hotelItemImagMapper.insert(imag);
        }

    }

    @Override
    public List<HotelItemSpu> getHotelItemAll() {
        //把全部的数据进行返回;
        List<HotelItemSpu> list = hotelItemSpuMapper.selectHotelItemAll();
        return list;
    }
}
